<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流水号" prop="flowNo">
        <el-input
          v-model="queryParams.flowNo"
          placeholder="请输入流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划编号" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作业名称" prop="workName">
        <el-input
          v-model="queryParams.workName"
          placeholder="请输入作业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验项" prop="checkItem">
        <el-input
          v-model="queryParams.checkItem"
          placeholder="请输入检验项"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产数量" prop="productCount">
        <el-input
          v-model="queryParams.productCount"
          placeholder="请输入生产数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采样数量" prop="sampleCount">
        <el-input
          v-model="queryParams.sampleCount"
          placeholder="请输入采样数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查值" prop="checkValue">
        <el-input
          v-model="queryParams.checkValue"
          placeholder="请输入检查值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验人" prop="checkPerson">
        <el-input
          v-model="queryParams.checkPerson"
          placeholder="请输入检验人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验时间" prop="checkTime">
        <el-date-picker clearable
                        v-model="queryParams.checkTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择检验时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:check:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:check:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:check:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:check:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品ID" align="center" prop="id"/>
      <el-table-column label="流水号" align="center" prop="flowNo"/>
      <el-table-column label="计划编号" align="center" prop="planNo"/>
      <el-table-column label="作业名称" align="center" prop="workName"/>
      <el-table-column label="物料名称" align="center" prop="materialName"/>
      <el-table-column label="规格" align="center" prop="quality"/>
      <el-table-column label="型号" align="center" prop="model"/>
      <el-table-column label="单位" align="center" prop="unit"/>
      <el-table-column label="检验项" align="center" prop="checkItem"/>
      <el-table-column label="生产数量" align="center" prop="productCount"/>
      <el-table-column label="采样数量" align="center" prop="sampleCount"/>
      <el-table-column label="检查值" align="center" prop="checkValue"/>
      <el-table-column label="阈值上限" align="center" prop="max"/>
      <el-table-column label="阈值下限" align="center" prop="min"/>
      <el-table-column label="检验结果" align="center" prop="checkResult">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_check_result" :value="scope.row.checkResult"/>
        </template>
      </el-table-column>
      <el-table-column label="检验人" align="center" prop="checkPerson"/>
      <el-table-column label="检验时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:check:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:check:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品检验对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="完工单号" prop="finishId">
          <div style="display: flex;justify-content: space-between">
            <el-input v-model="form.finishNo" placeholder="请输入完工单号" disabled/>
            <el-button type="primary" @click="handleSelectFinish">选择</el-button>
          </div>
        </el-form-item>
        <el-form-item label="作业名称" prop="workName">
          <el-input v-model="form.workName" placeholder="请输入作业名称"/>
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称"/>
        </el-form-item>
        <el-form-item label="采样数量" prop="sampleCount">
          <el-input v-model="form.sampleCount" placeholder="请输入采样数量"/>
        </el-form-item>
        <el-form-item label="检查值" prop="checkValue">
          <el-input v-model="form.checkValue" placeholder="请输入检查值"/>
        </el-form-item>
        <el-form-item label="检验结果" prop="checkResult">
          <el-select v-model="form.checkResult" placeholder="请选择检验结果">
            <el-option
              v-for="dict in dict.type.sys_check_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检验标准" prop="checkStandard">
          <div style="display: flex;justify-content: space-between">
            <el-input v-model="form.checkStandardName" placeholder="请输入检验标准" disabled/>
            <el-button type="primary" @click="handleSelectStandard">选择</el-button>
          </div>

        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="选择生产完工单号" :visible.sync="openFinish" width="500px" append-to-body>
      <finish @handle-finish="handleFinish"></finish>
    </el-dialog>

    <el-dialog :title="选择检验标准" :visible.sync="openStandard" width="500px" append-to-body>
      <standard @handle-standard="handleStandard"></standard>
    </el-dialog>

  </div>
</template>

<script>
import { listCheck, getCheck, delCheck, addCheck, updateCheck } from '@/api/system/check'
import Finish from '@/views/system/check/finish'
import Standard from '@/views/system/check/standard'
import { listStandard } from '@/api/system/standard'
import { listFinish } from '@/api/system/finish'

export default {
  name: 'Check',
  components: { Standard, Finish },
  dicts: ['sys_check_result'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品检验表格数据
      checkList: [],
      finishList: [],
      standardList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openFinish: false,
      openStandard: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowNo: null,
        planNo: null,
        workName: null,
        materialName: null,
        checkItem: null,
        productCount: null,
        sampleCount: null,
        checkValue: null,
        checkPerson: null,
        checkTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workName: [
          { required: true, message: '作业名称不能为空', trigger: 'blur' }
        ],
        materialName: [
          { required: true, message: '物料名称不能为空', trigger: 'blur' }
        ],
        sampleCount: [
          { required: true, message: '采样数量不能为空', trigger: 'blur' }
        ],
        checkValue: [
          { required: true, message: '检查值不能为空', trigger: 'blur' }
        ],
        finishId: [
          { required: true, message: '员工单号不能为空', trigger: 'blur' }
        ],
        checkStandard: [
          { required: true, message: '检验标准不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getFinishList()
    this.getStandardList()

  },
  methods: {
    handleFinish(row) {
      console.log(row)
      this.openFinish = false
      this.form.finishId = row.id
      this.form.finishNo = row.finishNo
    },
    handleStandard(row) {
      console.log(row)
      this.openStandard = false
      this.form.checkStandard = row.id
      this.form.checkStandardName = row.checkStandard
    },
    /** 查询产品检验列表 */
    getList() {
      this.loading = true
      listCheck(this.queryParams).then(response => {
        this.checkList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getFinishList() {
      listFinish().then(response => {
        this.finishList = response.rows;
      })
    },
    getStandardList() {
      listStandard().then(response => {
        this.standardList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        flowNo: null,
        planNo: null,
        workName: null,
        materialName: null,
        quality: null,
        model: null,
        unit: null,
        checkItem: null,
        productCount: null,
        sampleCount: null,
        checkValue: null,
        max: null,
        min: null,
        checkResult: null,
        checkPerson: null,
        checkTime: null,
        finishId: null,
        checkStandard: null,
        remark: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 多选框选中数据 **/
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加生产采样单'
    },
    handleSelectFinish() {
      this.openFinish = true
    },
    handleSelectStandard() {
      this.openStandard = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCheck(id).then(response => {
        console.log(this.form)
        console.log(this.standardList)
        console.log(this.finishList)
        this.form = response.data
        this.form.checkStandardName = this.standardList.find(s => s.id === this.form.checkStandard).checkStandard
        this.form.finishNo = this.finishList.find(s => s.id === this.form.finishId).finishNo
        this.open = true
        this.title = '修改产品检验'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCheck(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCheck(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除产品检验编号为"' + ids + '"的数据项？').then(function() {
        return delCheck(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/check/export', {
        ...this.queryParams
      }, `check_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
